package com.example.administrator.yunniuusedcar.holder;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.yunniuusedcar.adapter.MyBaseAdapter;
import com.example.administrator.yunniuusedcar.model.Contact;
import com.example.administrator.yunniuusedcar.R;

/**
 *
 * @author LQW
 */
public class ContactHolder extends BaseHolder<Contact> {

    private ImageView ivIcon;
    private TextView tvFirstLetter;
    private TextView tvName;

    // 构造方法
    public ContactHolder(Context context, ViewGroup parent,
                         MyBaseAdapter<Contact> adapter,
                         int position, Contact bean) {
        super(context, parent, adapter, position, bean);/////
    }

    // 创建列表项：填充列表项布局，查找子控件
    @Override
    public View onCreateView(Context context, ViewGroup parent) {
        View item = LayoutInflater.from(context)
                .inflate(R.layout.item_contact, parent, false);

        // 查找子控件
        tvFirstLetter = (TextView) item.findViewById(R.id.tv_first_letter);
        ivIcon = (ImageView) item.findViewById(R.id.iv_icon);
        tvName = (TextView) item.findViewById(R.id.tv_name);

        return item;
    }

    // 刷新列表项中子控件的显示
    @Override
    protected void onRefreshView(Contact bean, int position) {
        // 第一个列表项会显示首字母
        if (position == 0) {
            // 显示首字母
            tvFirstLetter.setVisibility(View.VISIBLE);
            tvFirstLetter.setText(bean.firstLetter);
        } else {
            // 获取上一个列表项的联系人数据
            Contact preContact = super.adapter.getItem(position - 1);
            // 当与上一个列表项的首字母不一样时才显示
            if (!bean.firstLetter.equals(preContact.firstLetter)) {
                // 显示首字母
                tvFirstLetter.setVisibility(View.VISIBLE);
                tvFirstLetter.setText(bean.firstLetter);
            } else {
                // 隐藏首字母
                tvFirstLetter.setVisibility(View.GONE);
            }
        }

        // 显示姓名
        tvName.setText(bean.name);


    }
}
